package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.entity.GoodsSku;
import com.micro.modual.entity.Prefecture;
import com.micro.modual.entity.PrefectureGoods;
import com.micro.modual.extension.PrefectureExtension;
import com.micro.modual.extension.PrefectureGoodsExtension;
import com.micro.modual.mapper.GoodsSkuMapper;
import com.micro.modual.mapper.PrefectureGoodsMapper;
import com.micro.modual.mapper.PrefectureMapper;
import com.micro.modual.model.bo.prefecturegoods.PrefectureGoodsBO;
import com.micro.modual.model.bo.prefecturegoods.PrefectureGoodsListBO;
import com.micro.modual.model.bo.prefecturegoods.PrefectureGoodsStatusBO;
import com.micro.modual.model.dto.goodssku.GoodsSkuListDTO;
import com.micro.modual.model.dto.prefecturegoods.PrefectureGoodsListDTO;
import com.micro.modual.service.PrefectureGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>专区商品表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("prefectureGoodsService")
public class PrefectureGoodsServiceImpl extends AbstractBasicsServiceImpl<PrefectureGoods,Long> implements PrefectureGoodsService {

    @Autowired
    private PrefectureMapper prefectureMapper;

    @Autowired
    private PrefectureGoodsMapper prefectureGoodsMapper;

    @Autowired
    private GoodsSkuMapper goodsSkuMapper;

    @Override
    public RespJsonPageData<PrefectureGoodsListDTO> findPrefectureGoodsList(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<PrefectureGoods> prefectureGoodsList = prefectureGoodsMapper.findPrefectureGoodsList(params);
        List<PrefectureGoodsListDTO> prefectureGoodsListDTOList = JacksonUtil.getList(JacksonUtil.toJson(prefectureGoodsList),PrefectureGoodsListDTO.class);
        return new RespJsonPageData<PrefectureGoodsListDTO>(prefectureGoodsListDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }
    
    /**
     * <h2>新增专区商品表详情</h2>
     * @param prefectureGoodsBO {@link PrefectureGoodsBO} 专区商品表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addPrefectureGoods(PrefectureGoodsBO prefectureGoodsBO) {
        List<PrefectureGoods> prefectureGoodsList = PrefectureGoodsExtension.convertAddPrefectureGoods(prefectureGoodsBO);
        prefectureGoodsMapper.insertListKeySelectiveMapper(prefectureGoodsList);
        Prefecture prefecture = prefectureMapper.findPrefectureByCode(prefectureGoodsBO.getPrefectureCode());
        Integer prefectureGoodsCount = prefectureGoodsBO.getPrefectureGoodsList().size();
        prefecture.setGoodsNumber(prefecture.getGoodsNumber() + prefectureGoodsCount);
        prefectureMapper.updateByPrimaryKeySelective(prefecture);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }


    /**
     * <h2>根据主键编号删除专区商品表信息</h2>
     * @param id  专区商品表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deletePrefectureGoods(Long id) {
        PrefectureGoods prefectureGoods = prefectureGoodsMapper.selectByPrimaryKey(id);
        Prefecture prefecture = prefectureMapper.findPrefectureByCode(prefectureGoods.getPrefectureCode());
        Integer goodsNumber = prefecture.getGoodsNumber() - 1;
        prefecture.setGoodsNumber(goodsNumber >= 0 ? goodsNumber:0);
        prefectureMapper.updateByPrimaryKeySelective(prefecture);
        prefectureGoodsMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJson modifyPrefectureGoodsStatus(PrefectureGoodsStatusBO prefectureGoodsStatusBO) {
        PrefectureGoods prefectureGoods = prefectureGoodsMapper.selectByPrimaryKey(prefectureGoodsStatusBO.getId());
        prefectureGoods.setGoodsStatus(prefectureGoodsStatusBO.getPrefectureGoodsStatus());
        prefectureGoodsMapper.updateByPrimaryKeySelective(prefectureGoods);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    @Override
    public RespJsonData<List<GoodsSkuListDTO>> findGoodsSkuAll(PrefectureGoodsListBO prefectureGoodsListBO) {
        List<String> skuCodeList = prefectureGoodsMapper.findPrefectureGoodsSkuCodeList(prefectureGoodsListBO.getPrefectureCode());
        List<GoodsSku> goodsSkuList = goodsSkuMapper.findGoodsSkuListByCategoryCode(prefectureGoodsListBO.getCategoryCode());
        List<GoodsSkuListDTO> skuListDTOList = PrefectureExtension.convertGoodsSkuListDTO(skuCodeList,goodsSkuList);
        return new RespJsonData<List<GoodsSkuListDTO>>(skuListDTOList, MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }


}
